package com.foreknow.test730;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-07-30 15:21
 */
public  class  Do {
    List<Student> students;

    /**
     * 初始化
     */
    public Do() {
        students = new ArrayList<>();
        students.add(new Student("1", "tom", 20));
        students.add(new Student("2", "jerry", 20));
        students.add(new Student("3", "zzzz", 20));

//        for (int i = 0; i < students.size(); i++) {
//            Student student = students.get(i);
//            System.out.println(student.getId() + "\t" + student.getName() + "\t"
//                    + student.getAge());
//        }
    }

    /**
     * 添加操作
     *
     * @param stu
     * @return
     */
    public boolean add(Student stu) {
        if (stu != null) {
            students.add(stu);
            return true;
        }
        return false;
    }

    /**
     * 根据id 删除
     * @param id
     * @return
     */
    public boolean del(String id){
        for (int i = 0; i < students.size(); i++) {
            if(id.equals(students.get(i).getId())){
                students.remove(i);
                return true;
            }

        }
        return false;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Student getStuById(String id){
        for (int i = 0; i < students.size(); i++) {
            if(id.equals(students.get(i).getId())){
                return students.get(i);
            }
        }
        return null;
    }


    public Student upDtedById(Student student){
        Scanner sc=new Scanner(System.in);
        System.out.print("学号"+"("+student.getId()+")");
        String newid=sc.next();
        System.out.print("姓名"+"("+student.getName()+")");
        String newname=sc.next();
        System.out.print("年龄"+"("+student.getAge()+")");
        int  newage=sc.nextInt();
        student.setName(newname);
        student.setId(newid);
        student.setAge(newage);
        return student ;

    }


    public List<Student> getAll(){
//        for (Student arr:students
//             ) {
//            System.out.println(arr.toString());
//        }
        return students;
    }
    public void show(List<Student> startstu){
        for (Student arr:startstu
        ) {
            System.out.println(arr.toString());
        }

            for (int i = 1; i < startstu.size(); i++) {
                for (int j = 0; j < i; j++) {
                    if(startstu.get(j).getId().equals(students.get(i).getId())){
                        throw new MyException("学号重复");
                    }
                }
            }

    }


}
